package com.app.controller;


import cn.hutool.core.util.IdUtil;
import com.app.common.Result;
import com.app.enums.StatusCodeEnum;
import com.app.model.UserModel;
import com.app.service.IPurchaseCouponsRecordService;
import com.app.service.UserService;
import com.app.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * <p>
 * 我的购劵记录表 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2024-09-03
 */
@RestController
@RequestMapping("/purchaseCouponsRecord")
public class PurchaseCouponsRecordController {

    @Autowired
    private IPurchaseCouponsRecordService purchaseCouponsRecordService;
    @Autowired
    private UserService userService;

    /**
     * 购买消费券
     */
    @GetMapping("/buyAcount")
    public Result buyAcount(BigDecimal money,HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.nothingness(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        return purchaseCouponsRecordService.buyAcount(money,userModel);
    }


    /**
     * 列表查询
     */
    @GetMapping("/getList")
    public Result getList(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        return purchaseCouponsRecordService.getList(userModel.getId());
    }
}

